package com.qf.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.common.page.PageInfo;
import com.qf.common.page.PageUtils;
import com.qf.common.response.Response;
import com.qf.entity.dto.Module;
import com.qf.entity.dto.ModuleRoleMiddle;
import com.qf.entity.dto.Role;
import com.qf.entity.po.LoginAdmin;
import com.qf.entity.vo.RoleAndMiddleList;
import com.qf.service.ModuleRoleMiddleService;
import com.qf.service.ModuleService;
import com.qf.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {

    @Resource
    ModuleService moduleService;
    @Resource
    RoleService roleService;
    @Resource
    ModuleRoleMiddleService moduleRoleMiddleService;

    /**
     * 前往权限管理页面，携带所有角色信息和当前登录者功能模块信息集合
     * @param model
     * @param loginAdmin
     * @return
     */
    @RequestMapping("list")
    public String toPermissionList(Model model,@RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage, LoginAdmin loginAdmin){
        QueryWrapper<Role> roleWrapper = new QueryWrapper<>();
        roleWrapper.lambda().orderByDesc(Role::getCreateTime);
        PageInfo<Role> pageInfo = PageUtils.getPageList(currentPage, roleWrapper, roleService);
        model.addAttribute("roleList",pageInfo.getPageRecords());
        model.addAttribute("pageCounts",pageInfo.getPageCounts());
        model.addAttribute("moduleList",moduleService.getModuleListByAdminId(loginAdmin.getId()));
        return "permission/list";
    }

    /**
     * 添加或修改角色信息
     * @param role
     * @param moduleRoles
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    public Response<?> save(Role role,@RequestParam("moduleRoles")String moduleRoles) {
        roleService.saveRole(role, moduleRoles);
        return Response.success();
    }

    /**
     * 删除角色信息
     * @param id
     * @return
     */
    @PostMapping("del")
    @ResponseBody
    public Response<?> del(@RequestParam("id")Long id) {
        roleService.removeById(id);
        moduleRoleMiddleService.removeByRoleId(id);
        return Response.success();
    }

    /**
     * 根据角色id查找该角色对象
     * @param roleId
     * @return
     */
    @PostMapping("getById")
    @ResponseBody
    public Response<?> getRoleAndModuleList(@RequestParam("id")Long roleId) {
        Role role = roleService.getById(roleId);
        List<ModuleRoleMiddle> middleList = moduleRoleMiddleService.getMiddleListByRoleId(roleId);
        RoleAndMiddleList roleAndMiddleList = new RoleAndMiddleList(role,middleList);
        return Response.success(roleAndMiddleList);
    }
}
